﻿using System;
using System.Linq.Expressions;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Model;
using RedRiver.Framework.XtraCommon.Extensions;

namespace RedRiver.Framework.XtraCommon.RichEdit{
    public partial class RichEditForm : RibbonForm{
        private bool _isDataChanged;

        public RichEditForm(){
            InitializeComponent();
            WindowState = FormWindowState.Maximized;
            richEditControl.ModifiedChanged += (s, e) => _isDataChanged = true;
        }

        /// <summary>
        /// 数据是否改变并保存
        /// <remarks>
        /// 如果返回结果为 True 则需要重新加载
        /// 当数据改变并且提交成功后,返回值为True,否则为False
        /// </remarks>
        /// </summary>
        /// <returns></returns>
        public bool IsDataChanged{
            get { return _isDataChanged; }
        }

        public RichEditControl RichEdit{
            get { return richEditControl; }
        }

        /// <summary>
        /// 邮件数据合并数据源
        /// <see cref="MailMergeOptions.DataSource"/>
        /// </summary>
        public object MailMergeDataSource{
            get { return richEditControl.Options.MailMerge.DataSource; }
            set{
                if (value is BindingSource){
                    richEditControl.Options.MailMerge.DataSource = value;
                } else{
                    richEditControl.Options.MailMerge.DataSource = new BindingSource{
                                                                                        DataSource = value
                                                                                    };
                }
            }
        }

        public void BindingData<TModel, TValue>(TModel m, DocumentFormat bindingFormat,
                                                Expression<Func<TModel, TValue>> expression)
            where TModel : class{
            richEditControl.DataBindings.Clear();
            defaultBindingSource.DataSource = m;
            richEditControl.DataBindings.Add(new Binding(GetBindingPropertyName(bindingFormat), defaultBindingSource,
                                                         expression.MemberName(), true));
            //new System.Windows.Forms.Binding("RtfText", this.defaultBindingSource, "Content", true)
            fileRibbonPage1.Visible = false;
            _isDataChanged = false;
        }

        private static string GetBindingPropertyName(DocumentFormat bindingFormat){
            if (bindingFormat == DocumentFormat.Doc){
                return "RtfText";
            }
            if (bindingFormat == DocumentFormat.OpenDocument){
                return "OpenDocumentBytes";
            }
            if (bindingFormat == DocumentFormat.OpenXml){
                return "OpenXmlBytes";
            }
            if (bindingFormat == DocumentFormat.Rtf){
                return "RtfText";
            }
            if (bindingFormat == DocumentFormat.Html){
                return "HtmlText";
            }
            if (bindingFormat == DocumentFormat.Mht){
                return "MhtText";
            }
            if (bindingFormat == DocumentFormat.PlainText){
                return "Text";
            }
            if (bindingFormat == DocumentFormat.WordML){
                return "WordMLText";
            }
            return "Text";
        }
    }
}